package com.jyy.utils.dynamicdatasource;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 数据源配置类
 *
 * @author jyy
 * @date 2025-01-13
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "dynamic")
public class DynamicProperties {

    private String url;

    @Value("${dynamic.driver-class-name}")
    private String driverClassMame;

    private String username;

    private String password;

    private String urlSuffix;

    /**
     * 获取数据库连接地址
     *
     * @param databaseName 数据库名称
     * @return 数据库连接地址
     */
    public String getJdbcUrl(String databaseName) {
        // 驱动区分
        switch (driverClassMame) {

            // sqlserver
            case DynamicConstant.SQLSERVER_DRIVE: {
                // jdbc:sqlserver://127.0.0.1:1433;databaseName=MyDatabase;
                return url + "databaseName=" + databaseName + ";";
            }

            // mysql
            case DynamicConstant.MYSQL_DRIVE: {
                // jdbc:mysql://127.0.0.1:3306/jyy-test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
                return url + databaseName + urlSuffix;
            }

            default:
                log.error("数据源类型不存在");
                return null;
        }
    }
}
